import {ScalarType} from "./definition";

export class Field {

  name: string;
  description?: string;
  type: ScalarType;

  // plugins: Plugin[] = [];

  constructor(config: FieldConfigInternal) {
    this.name = config.name;
    this.description = config.description;
    this.type = config.type;
  }

  toConfig() {
    return {
      name: this.name,
      type: this.type.toString(),
      description: this.description,
      // plugins: this.plugins.map(plugin => plugin.toConfig())
    }
  }

}

interface FieldConfigInternal {
  name: string;
  description?: string;
  type: ScalarType;
}

export interface FieldConfig {
  name?: string;
  description?: string;
  type: ScalarType;
}

export const fieldKey = '__FIELD_KEY__';

export function field(fieldConfig: FieldConfig): PropertyDecorator {
  return function (target, propertyKey) {
    const value = Reflect.getMetadata(fieldKey, target.constructor) || [];
    const self = new Field({
      ...fieldConfig,
      name: fieldConfig.name || propertyKey as string,
    });
    Reflect.defineMetadata(fieldKey, [...value, self], target.constructor);
  }
}
